<?php

namespace App\Admin\Controllers;

use App\Models\OrdersModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class OrdersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'OrdersModel';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrdersModel());

        $grid->column('id', __('Id'));
        $grid->column('c_account', __('卡劵号'));
        $grid->column('name', __('用户名'));
        $grid->column('tel', __('用户手机号'));
        $grid->column('province', __('省'))->display(function ($province){
            $province_id=$this->province;
            $province_ob=DB::table("china_area")->where('id',$province_id)->first();
            $province=$province_ob->name;
            return $province;
        });
        $grid->column('city', __('市'))->display(function ($city){
            $city_id=$this->city;
            $city_ob=DB::table("china_area")->where('id',$city_id)->first();
            $city=$city_ob->name;
            return $city;
        });
        $grid->column('area', __('区|县'))->display(function ($area){
            $area_id=$this->area;
            if ($area_id==0){
                return "";
            }else{
                $area_ob=DB::table("china_area")->where('id',$area_id)->first();
                $area=$area_ob->name;
                return $area;
            }
        });
        $grid->column('address', __('详细地址'));
        $grid->column('order_mark', __('订单编号'));
        $grid->column('tracking_number', __('物流单号'))->display(function ($tracking_number){
            if($tracking_number==null){
                return "<span style='color: red'>未发货</span>";
            }else{
                return $this->tracking_number;
            }
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('订单更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrdersModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('c_account', __('C account'));
        $show->field('name', __('Name'));
        $show->field('tel', __('Tel'));
        $show->field('province', __('Province'));
        $show->field('city', __('City'));
        $show->field('area', __('Area'));
        $show->field('address', __('Address'));
        $show->field('order_mark', __('Order mark'));
        $show->field('tracking_number', __('Tracking number'));
        $show->field('create_time', __('Create time'));
        $show->field('up_time', __('Up time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OrdersModel());

        $form->text('c_account', __('C account'));
        $form->text('name', __('Name'));
        $form->text('tel', __('Tel'));
        $form->text('province', __('Province'));
        $form->text('city', __('City'));
        $form->text('area', __('Area'));
        $form->text('address', __('Address'));
        $form->text('order_mark', __('Order mark'));
        $form->text('tracking_number', __('Tracking number'));
        $form->number('created_at', __('Create time'));
        $form->number('updated_at', __('更新时间'));

        return $form;
    }
}
